// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import './category.dart';
import './home.dart';
import './setting.dart';
import './mine.dart';

void main() {
  runApp(const MaterialApp(
    home: MyText1(),
  ));
}

class MyText extends StatefulWidget {
  const MyText({super.key});

  @override
  State<MyText> createState() => _MyTextState();
}

class _MyTextState extends State<MyText> {
  int num = 10;
  void Function()? onPressed() {
    setState(() {
      num++;
    });
    return null;
  }

  List<Widget> renderList() {
    List<Widget> list = [];
    for (var k = 0; k < num; k++) {
      list.add(ListTile(title: Text('这是一条列表项$k')));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('这是标题')),
      body: ListView(
        children: [
          ...renderList(),
        ],
      ),
      bottomNavigationBar: const BottomButton(),
    );
  }
}

class BottomButton extends StatefulWidget {
  const BottomButton({super.key});

  @override
  State<BottomButton> createState() => _BottomButtonState();
}

class _BottomButtonState extends State<BottomButton> {
  int selectedIndex = 1;

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      currentIndex: selectedIndex,
      onTap: (v) {
        if (selectedIndex != v) {
          setState(() {
            selectedIndex = v;
          });
        }
      },
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
        BottomNavigationBarItem(icon: Icon(Icons.message), label: '消息'),
        BottomNavigationBarItem(icon: Icon(Icons.category), label: '菜单'),
      ],
    );
  }
}

class MyText1 extends StatefulWidget {
  const MyText1({super.key});

  @override
  State<MyText1> createState() => _MyText1State();
}

class _MyText1State extends State<MyText1> {
  int selectedIndex = 0;

  List<Widget> renderList = const [
    HomePage(),
    SettingPage(),
    CategoryPage(),
    MinePage()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('这是title')),
      body: renderList[selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: selectedIndex,
        onTap: (v) {
          if (selectedIndex != v) {
            setState(() {
              selectedIndex = v;
            });
          }
        },
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.settings), label: '设置'),
          BottomNavigationBarItem(icon: Icon(Icons.category), label: '菜单'),
          BottomNavigationBarItem(
              icon: Icon(Icons.my_library_add), label: '我的'),
        ],
      ),
    );
  }
}
